home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9486 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.0 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Segmentation Fault ???
  5. Date: 10 Mar 1996 13:56:44 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hvj6sINN9gl@keats.ugrad.cs.ubc.ca>
  8. References: <4hsa7i$en3@wraith.its.uow.edu.au> <4huis1$cm2@ccshst05.cs.uoguelph.ca> <4hv75jINNpss@keats.ugrad.cs.ubc.ca> <4hvaq3$n8f@ccshst05.cs.uoguelph.ca>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4hvaq3$n8f@ccshst05.cs.uoguelph.ca>,
  12. Toby K Hay <thay@uoguelph.ca> wrote:
  13.  >Kazimir Kylheku (c2a192@ugrad.cs.ubc.ca) wrote:
  14.  >
  15.  >: It means that you are making errors in your code that the Turbo C environment
  16.  >: doesn't catch. The bus error is likely caused by invoking
  17.  >: implementation-specific behavior that is in contravention to standard C:
  18.  >: converting a pointer to one that has a stricter alignment. On many of the
  19.  >: processors used in UNIX workstations, the address of a long word has to be
  20.  >: divisible by four. On a 68000 processor, the address of a 16-bit word has to be
  21.  >: divisible by two.
  22.  >: If you fail to meet these alignment restrictions, the hardware will trigger an
  23.  >: exception, and the UNIX kernel will send a SIGBUS signal to your program.
  24.  >
  25.  >Would lint catch this for me, or will I have to learn about alignment 
  26.  >restrictions to run my program?
  27.  
  28. I'm ashamed to admit that I have never used lint except out of curiosity. Using
  29. the GNU C compiler with all warnings turned on usually turns up some dirt. By
  30. all means, try lint to see what it comes up with! It certainly won't hurt.
  31.  
  32. Alignment violations are not something that you should need to comb your code
  33. for, unless you have written a great deal of code on your PC that makes such
  34. violations!
  35.  
  36. It's not something that happens by accident; it's usually the result of an
  37. explicit pointer conversion from one type to another.  Such conversions are
  38. almost always a bad idea. You can only convert a strongly aligned type to a
  39. less strong one. The char * always has the most relaxed alignment, though. So
  40. you can safely convert an int * to a char *, but not vice versa.
  41.  
  42. Your compiler should be able to diagnose such conversions, unless they are done
  43. through void * as an intermediary. Turn on all warnings. Try using 'gcc' if it
  44. is available, with the arguments -Wall -pedantic.  If it is not available, have
  45. the admin install it ASAP! :)
  46.  
  47. Also, the bus error may indicate other types of undefined or
  48. implementation-defined behavior. If you are converting between int and char
  49. pointers, you may also be making assumptions about the byte order, the size of
  50. an integer or long and the like. In particular, using a character pointer to
  51. access an integer object is a very bad thing!
  52.  
  53. If you wrote the code, you probably have a good idea where you did these kinds
  54. of things. If you heavily depend on implementation-defined pointer behavior,
  55. you may have to re-think and re-write portions of your code to make it more
  56. portable.
  57.  
  58.  >From three replies I received via E-mail I understand that accessing 
  59.  >illegal memory is almost certainly caused by using uninitialized pointers 
  60.  >- something I will check my code for again.  But what determines the 
  61.  
  62. Uninitialized pointers, forgetting arguments to unprototyped functions,
  63. out-of-bounds array references, forgetting to check for NULL values from
  64. functions that _can_ return NULL...  all of these are potential segfault
  65. candidates.  
  66.  
  67.  >limits of my malloc heap, stack, and so forth?  Can I request more memory 
  68.  >for these at the command line when I start the program?
  69.  
  70. You don't have to. Modern UNIX automagically adds memory to your address space
  71. where no memory existed before. Normally, this means you can malloc() as much
  72. as you want without worrying about hitting the wall. When your program exits,
  73. the space is returned to the operating system.
  74.  
  75. There may be administrator-imposed per-user process limits on parameters such
  76. as maximum size of a task. If not, the available RAM + swap space is usually
  77. the limit on how much you can allocate. 
  78.  
  79. -- 
  80.  
  81.